<?php
namespace user;
use View;
use Response;
use BaseController;
use Auth;
use User;
use Input;
use Validator;
use Redirect;

class UserController extends BaseController {

	/*
	|--------------------------------------------------------------------------
	| Default Home Controller
	|--------------------------------------------------------------------------
	|
	| You may wish to use controllers instead of, or in addition to, Closure
	| based routes. That's great! Here is an example controller method to
	| get you started. To route to this controller, just add the route:
	|
	|	Route::get('/', 'HomeController@showWelcome');
	|
	*/

	public function index(){
		return View::make('user.index')->with('title','用户管理');
	}

	/**
	 * 修改头像页面
	 */
	public function avatar(){
		$user = User::find(Auth::id());
		return View::make('user.user.avatar')->with('avatar',$user->avatar_b)->with('title','头像修改');
	}

	/**
	 * 保存头像修改页面
	 */
	public function doAvatar(){

		$savePath = public_path().'/uploads/avatar/';  //图片存储路径
		$savePicName = Auth::user()->username.'_'.time(); //图片存储名称

		$filenamesrc = $savePath.$savePicName."_src.jpg";
		$filename162 = $savePath.$savePicName."_162.jpg";
		$filename48 = $savePath.$savePicName."_48.jpg";
		$filename20 = $savePath.$savePicName."_20.jpg";

		$src=base64_decode($_POST['pic']);
		$pic1=base64_decode($_POST['pic1']);
		$pic1=base64_decode($_POST['pic1']);
		$pic2=base64_decode($_POST['pic2']);
		$pic3=base64_decode($_POST['pic3']);

		if(file_put_contents($filename162,$pic1) && file_put_contents($filename48,$pic2) && file_put_contents($filename20,$pic3) && file_put_contents($filenamesrc,$src)){

			//更新到数据库
			$user = User::find(Auth::id());
			$user->avatar_b = 'uploads/avatar/'.$savePicName."_162.jpg";
			$user->avatar_m = 'uploads/avatar/'.$savePicName."_48.jpg";
			$user->avatar_s = 'uploads/avatar/'.$savePicName."_20.jpg";

			if($user->save()){
				$rs['status'] = 1;
				$rs['picUrl'] = $savePath.$savePicName;
			}else{
				$rs['status'] = -1;
				$rs['picUrl'] = '';
			}

		}else{
			$rs['status'] = -1;
			$rs['picUrl'] = '';
		}

		return Response::json($rs);
	}

	/**
	 * 个人信息修改页面
	 * @return [type] [description]
	 */
	public function profile(){
		$user = User::find(Auth::id());
		return View::make('user.user.profile')->with('user',$user)->with('title','个人信息修改');
	}

	/**
	 * 个人信息修改保存
	 * @return [type] [description]
	 */
	public function doProfile(){
		$rules = array(
			'signature'=>'max:100',
			'weekly_report'=>'boolean',
			'notify'=>'boolean'
		);

		$validator = Validator::make(Input::all(),$rules);
		if($validator->passes()){
			$birth = explode('-',Input::get('birth'));
			$user = User::find(Auth::id());
			$user->signature = strip_tags(Input::get('signature'));
			$user->birth_year = $birth[0];
			$user->birth_month = $birth[1];
			$user->birth_day = $birth[2];
			$user->weekly_report = (int) Input::get('weekly_report');
			$user->notify = (int) Input::get('nofify');
			$user->nickname = Input::get('nickname');

			if($user->save()){
				return Redirect::back()->with('message',array('type'=>'success','content'=>'修改成功'));
			}else{
				return Redirect::back()->with('message',array('type'=>'danger','content'=>'修改失败，请重试'))->withInput();
			}
		}else{
			return Redirect::back()->withInput()->withErrors($validator);
		}
	}

}
